﻿#ifndef _PLOT111_
#define _PLOT111_
#include  <QMutex>
#include  <qwt/qwt_plot.h>
#include "include/global.h"

QT_BEGIN_NAMESPACE
class QwtPlotPicker;
class QwtPlotCurve;
class QwtPlotGrid;
QT_END_NAMESPACE

#define DISPLAY_POINT_NUM 11


class Plot : public QwtPlot
{
    Q_OBJECT
public:
    Plot( QWidget * = 0 );
    ~Plot();
	void setYAxisScale(int m, int n);
    const QwtPlotCurve *getCurve() const{return curve;}
    /*!
     \breif 根据串口数据更新存储
     \param index为索引，f为数据
    */

    void setDataByIndex(long station, float f)//某采集站数据更新
    {
		//qDebug()<<"Current data"<<f<<endl;
        mutex.lock();
        for(int i = DISPLAY_POINT_NUM - 1; i > 0; --i)
            data[station][i] = data[station][i - 1];
        data[station][0] = static_cast<double>(f);
        mutex.unlock();

        if(dataCnt < DISPLAY_POINT_NUM)
            ++dataCnt;
    }
private Q_SLOTS:
    //! [2]
    /*!
      \breif 根据总线更新的时间来推动波形前进
      \param 当前总线时间
    */
public:
    void setIndexByTime(long t)
    {
		//qDebug()<<"Current station"<<currentStationNums<<"   "<<currentStation<<endl;
        if(mutex.tryLock())
        {
			#if 1
			for (int j= 0 ; j < currentStationNums; ++j)//推动现有采集站数据刷新
			{
				for(int i = DISPLAY_POINT_NUM - 1; i > 0; --i){
					data[j][i] = data[j][i-1];
					//data[j][0] = data[j][1];
				}
			}
#endif

#ifdef DEBUG
            using namespace  std;
            for(int i = DISPLAY_POINT_NUM - 1; i >= 0; --i)
                cout<<data[i]<<"  ";
#endif
            if(dataCnt < DISPLAY_POINT_NUM)
                ++dataCnt;
			paintPlotByTime(currentStation,t);
            mutex.unlock();
        }
        else
            return;
        for(int j = DISPLAY_POINT_NUM -1; j >= 0; --j)
            x[j] = t - j;
    }

     //!~ [2]
    void paintPlotByTime(int, long);//采集站+时间
    void setYLabel(const QString& title);
	void setPenColor(int k);

	void setCurrentStation(int s){currentStation = s;}
	int getCurrentStation() const {return currentStation;}
	void setCurrentStationNums(int s){currentStationNums = s;}
	int getCurrentStationNums() const {return currentStationNums;}

/*!
*/
private:
    int dataCnt;
    //x轴
    double x[DISPLAY_POINT_NUM];
    //放入栈中，有助于提高速度
    QMutex mutex;

    QwtPlotCurve *curve;
    //y值，Warning：数组太大，出现RUNTIMEERROR
    double data[MAX_STATIONS_NUMS][DISPLAY_POINT_NUM];
    //坐标轴
    QwtPlotPicker *d_picker;
    QwtPlotGrid *d_grid;
	int currentStation;
	int currentStationNums;
};
#endif
